import { defineStore } from 'pinia'
import requestCategoryList from '@/api/categoryList'
import { ElMessage } from 'element-plus'
interface stateModel {
    categoryL1List: any[]
    categoryL2List: any[]
    currentCategory: any
    categoryGroupList: any[]
    categoryItems: any
    categoryL1: any
    NewCategoryL2List: any[]
}
const initState = (): stateModel => ({
    categoryL1List: [],
    categoryL2List: [],
    currentCategory: {},
    categoryGroupList: [],
    categoryItems: {},
    categoryL1: {},
    NewCategoryL2List: []
})

export const useCategoryStore = defineStore('getCategoryList', {
    state() {
        return initState();
    },
    actions: {
        //首次渲染
        async getGoodsList() {
            try {
                let result = await requestCategoryList.getGoodsList()
                this.categoryL1List = result.data.categoryL1List
                // console.log(result.data);
            } catch (error) {
                ElMessage.error('获取首页数据失败')
                return Promise.reject(error);
            }
        },
        //点击导航获取请求
        async getSecondGoodsList(id: number) {
            try {
                let result = await requestCategoryList.getSecondGoodsList(id)
                this.categoryL2List = result.data.categoryL2List
                this.currentCategory = result.data.currentCategory
                this.categoryGroupList = result.data.categoryGroupList
                // console.log(result.data);
            } catch (error) {
                ElMessage.error('获取分类导航数据失败')
                return Promise.reject(error);
            }
        },
        //获取商品详细
        async getGoodsDetailList(subCategoryId: any, categoryId: any) {
            try {
                let result = await requestCategoryList.getGoodsDetailList(subCategoryId, categoryId)
                this.categoryItems = result.data.categoryItems
                this.categoryL1 = result.data.categoryL1
                this.NewCategoryL2List = result.data.categoryL2List
                console.log('跳转数据', result.data);
            } catch (error) {
                // alert(error)
                ElMessage.error('获取分类详细数据失败')
                return Promise.reject(error);
            }
        }
    }
})